﻿using LianXi1013.Entity.User;
using LianXi1013.Service.User;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;

namespace LianXi1013.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserInforService userInforService;

        public UserController(IUserInforService userInforService)
        {
            this.userInforService = userInforService;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet("LoginUser")]
        public IActionResult LoginUser(string name,string password)
        {
            return Ok(userInforService.LoginUser(name, password));
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost("LoginUser")]
        public IActionResult AddUser(UserInfor u)
        {
            return Ok(userInforService.AddUser(u));
        }

        /// <summary>
        /// 显示用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="minTime"></param>
        /// <param name="maxTime"></param>
        /// <returns></returns>
        [HttpGet("GetUser")]
        public IActionResult GetUser(string name, DateTime? minTime, DateTime? maxTime, int pageIndex, int pageSize)
        {
            return Ok(userInforService.GetUser(name, minTime, maxTime,pageIndex,pageSize));
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("AddPic")]
        public string AddPic(IFormFile file)
        {
            string picName = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000, 9999) + Path.GetFileName(file.FileName);
            string path = Directory.GetCurrentDirectory() + "/wwwroot/" + picName;
            using(FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return picName;
        }
    }
}
